package com.joker.dynamic.thread.pool.sdk.trigger.job;

import com.joker.dynamic.thread.pool.sdk.domain.IDynamicThreadPoolService;
import com.joker.dynamic.thread.pool.sdk.domain.model.entity.ThreadPoolConfigEntity;
import com.joker.dynamic.thread.pool.sdk.register.IRegistry;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;

/**
 * 上报动态线程池信息到 redis
 * Created By guzq on 2024/10/20
 */
@Slf4j
public class ThreadPoolDataReportJob {

    private final IDynamicThreadPoolService dynamicThreadPoolService;

    private final IRegistry registry;

    public ThreadPoolDataReportJob(IDynamicThreadPoolService dynamicThreadPoolService, IRegistry registry) {
        this.dynamicThreadPoolService = dynamicThreadPoolService;
        this.registry = registry;
    }


    @Scheduled(cron = "0/20 * * * * ?") // 每 20s 上报一次
    public void execReportThreadPoolList() {
        List<ThreadPoolConfigEntity> theadPoolConfigEntities = dynamicThreadPoolService.queryThreadPoolConfigList();
        registry.reportThreadPool(theadPoolConfigEntities);
        log.info("动态线程池 上报线程池信息");

        for (ThreadPoolConfigEntity theadPoolConfigEntity : theadPoolConfigEntities) {
            registry.reportThreadPoolConfigParameter(theadPoolConfigEntity);
            log.info("动态线程池 上报线程池配置 poolName:{}", theadPoolConfigEntity.getThreadPoolName());
        }
    }
}
